package com.example.askto;

import java.awt.image.BufferedImage;

/**
 * 屏幕截图调整
 */
public class ImageUtil {


    // 转换成灰度图

    public static BufferedImage toGray(BufferedImage image) {

        int width = image.getWidth();

        int height = image.getHeight();

        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        for (int i = 0; i < width; i++) {

            for (int j = 0; j < height; j++) {

                int rgb = image.getRGB(i, j);

                grayImage.setRGB(i, j, rgb);

            }

        }

        return grayImage;

    }

    /**
     * 计算相似度
     */
    public static double getSimilarity2Gray(BufferedImage image1, BufferedImage image2) {

        BufferedImage grayImage1 = toGray(image1);
        BufferedImage grayImage2 = toGray(image2);

        int width = grayImage1.getWidth();

        int height = grayImage1.getHeight();

        int count = 0;

        double similarity = 0;

        for (int i = 0; i < width; i++) {

            for (int j = 0; j < height; j++) {

                int rgb1 = grayImage1.getRGB(i, j);

                int rgb2 = grayImage2.getRGB(i, j);

                if (rgb1 == rgb2) {

                    count++;

                }

            }

        }

        similarity = (double) count / (width * height);

        return similarity;

    }
}
